<template>
  <!-- close是dialog提供的方法 -->
  <el-dialog :visible="showDialog" title="新增员工" @close="closeFn">
    <!-- 表单，添加表单校验规则 model，rules，prop，v-model -->
    <el-form ref="form" :model="formData" :rules="rules" label-width="120px">
      <el-form-item label="姓名" prop="username">
        <el-input v-model="formData.username" style="width:50%" placeholder="请输入姓名" />
      </el-form-item>
      <el-form-item label="手机" prop="mobile">
        <el-input v-model="formData.mobile" style="width:50%" placeholder="请输入手机号" />
      </el-form-item>
      <el-form-item label="入职时间" prop="timeOfEntry">
        <el-date-picker v-model="formData.timeOfEntry" style="width:50%" placeholder="请选择入职时间" />
      </el-form-item>
      <el-form-item label="聘用形式" prop="formOfEmployment">
        <el-select v-model="formData.formOfEmployment" style="width:50%" placeholder="请选择">
          <!-- 获取下拉框数据，根据聘用形式进行渲染 -->
          <!-- value属性的值和formOfEmployment变量双向绑定在一起 -->
          <el-option
            v-for="item in hireType"
            :key="item.id"
            :label="item.value"
            :value="item.id"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="工号" prop="workNumber">
        <el-input v-model="formData.workNumber" style="width:50%" placeholder="请输入工号" />
      </el-form-item>
      <el-form-item label="部门" prop="departmentName">
        <!-- @focus点击输入框，显示部门列表 -->
        <el-input
          v-model="formData.departmentName"
          style="width:50%"
          placeholder="请选择部门"
          @focus="showDept=true"
        />
        <!-- default-expand-all展示所有子节点
        node-click 节点被点击时的回调-->
        <el-tree
          v-show="showDept"
          :data="depts"
          :props="{label: 'name'}"
          default-expand-all
          @node-click="selectNodeFn"
        />
      </el-form-item>
      <el-form-item label="转正时间">
        <el-date-picker v-model="formData.correctionTime" style="width:50%" placeholder="请选择转正时间" />
      </el-form-item>
    </el-form>
    <!-- footer插槽 -->
    <template v-slot:footer>
      <el-row type="flex" justify="center">
        <el-col :span="6">
          <el-button size="small" @click="closeFn">取消</el-button>
          <el-button type="primary" size="small" @click="confirmFn">确定</el-button>
        </el-col>
      </el-row>
    </template>
  </el-dialog>
</template>

<script>
// 引入聘用形式，并声明在data中
import employeesData from '@/api/constant/employees'
import { getDepartments } from '@/api/departments'
import { listToTreeDate } from '@/utils/index'
import { addEmployee } from '@/api/employees'
export default {
  props: {
    showDialog: {
      type: Boolean,
      default: false
    }
  },
  data() {
    return {
      hireType: employeesData.hireType,
      formData: {
        username: '',
        mobile: '',
        formOfEmployment: '',
        workNumber: '',
        departmentName: '',
        timeOfEntry: '',
        correctionTime: ''
      },
      rules: {
        username: [
          { required: true, message: '姓名不能为空', trigger: 'blur' },
          { min: 1, max: 4, message: '名字长度为1-4', tirgger: 'blur' }
        ],
        mobile: [
          { required: true, message: '手机号不能为空', trigger: 'blur' },
          { pattern: /1[3-9]\d{9}$/, message: '手机号码格式不正确', tirgger: 'blur' }
        ],
        formOfEmployment: [
          { required: true, message: '聘用形式不能为空', trigger: 'blur' }
        ],
        workNumber: [
          { required: true, message: '工号不能为空', trigger: 'blur' }
        ],
        departmentName: [
          { required: true, message: '部门名称不能为空', trigger: 'change' }
        ],
        timeOfEntry: [
          { required: true, message: '入职时间不能为空', trigger: 'blur' }
        ]
      },
      depts: [],
      showDept: false // 控制部门列表的显示隐藏
    }
  },
  async created() {
    // 获取部门列表
    const res = await getDepartments()
    // 树形结构
    this.depts = listToTreeDate(res.depts, '')
    // console.log(73, res)
  },
  methods: {
    // 确认 新增
    async confirmFn() {
      // 表单验证
      await this.$refs.form.validate()
      // 调用接口
      await addEmployee(this.formData)
      // 关闭弹窗
      this.closeFn()
      // 刷新列表
      this.$emit('updateEvent')
    },
    // 关闭弹窗
    closeFn() {
      // 清空数据
      this.formData = {
        username: '',
        mobile: '',
        formOfEmployment: '',
        workNumber: '',
        departmentName: '',
        timeOfEntry: '',
        correctionTime: ''
      }
      //   清空校验
      this.$refs.form.resetFields()
      this.$emit('update:showDialog', false)
    },
    // 选择部门
    selectNodeFn(obj) {
      this.formData.departmentName = obj.name
      // 控制部门列表的显示隐藏
      this.showDept = false
    }
  }
}
</script>

<style scoped>
.el-tree{
    position: absolute;
    width: 100%;
    height: 200px;
    z-index: 1;
    overflow: auto;
}
</style>
